<?php
class Asig_usuController extends Controller
{
    var $widKey = false;
    public function actionCheck_data()
    {

    }
    public function actionSave_data()
    {        
        $jsonData = json_decode($_REQUEST['allData']);
        $transaction = Yii::app()->db->beginTransaction();
        try{
            foreach($jsonData as $data){
                $modelAsig = tbl_asignacion_usuarios::model()->find('asigu_mun=:keyMun && asigu_usu=:keyUsu',array(':keyMun'=>$data->asigu_mun, ':keyUsu'=>$data->asigu_usu));
                if(empty($modelAsig->asigu_id)){
                    $model = new tbl_asignacion_usuarios();
                    $model->asigu_mun = $data->asigu_mun;
                    $model->asigu_usu = $data->asigu_usu;
                    $model->asigu_observacion = '';
                    $this->widKey = $model->save();
                }
                $model = tbl_municipio::model()->find("mun_id=:keyMun", array(':keyMun'=>$data->asigu_mun));                
                $model->mun_register = 1;
                $this->widKey = $model->save();
            }
            if($this->widKey){
                $transaction->commit();
                echo true;
            }                
        }catch(Exception $e){
            $this->widKey = false;
        }
        if(!$this->widKey){
            $transaction->rollback();
            echo "Error system controller 'Asig_usu' in the action -> Add.";
        }
    }

    public function actionDelete_data()
    {
        $transaction = Yii::app()->db->beginTransaction();
        try{
            $sql="delete from tbl_asignacion_usuarios where asigu_id=".$_GET['asigu_id'];
            $model = Yii::app()->db->createCommand($sql)->execute();
            if($model){
                $transaction->commit();
                echo "Eliminado";
            }
        }catch(Exception $e){
            $transaction->rollback();
            echo "Error system controller 'Asig_usu' in the action -> Delete.";
        }        
    }
	//grid de listado de asignacion de usuarios a municipios y sus respectivas boletas
    public function actionasigUMBGrid_data()
    {        
        $data = '';
        $cont = 0;
        $bolData = tbl_boleta::model()->findAll();        
        foreach($bolData as $bol){
            //echo 
            $asiData = tbl_asignacion_usuarios::model()->with('user','muni')->findAll('asigu_usu=:getId',array(':getId'=>$_GET['usu_id']));
            foreach($asiData as $row){
                $cont++;
                $data[] = array('id'=>$cont,'data'=>array(
                    $cont,
                    $bol->bol_tipo,
                    ($bol->bol_tipo == 'BINF')?'Boleta(s) de Infraestructura':'Boleta(s) de Gestión',
                    $row->user->usu_codigo,
                    $row->muni->dpto->dep_detalle,
                    $row->muni->mun_codigo,
                    $row->muni->mun_detalle                                                            
                ));               
            }
        }
        $allData = array('rows'=>$data);
        echo 'data = '.CJSON::encode($allData);        
    }
    public function actionLoadModuleAsig(){        
        $asiData = tbl_resp_modulos::model()->with('rpta','modu')->findAll('rma_municipio=:getMun && rma_tipobol=:getBol',array(':getMun'=>$_GET['muni'],':getBol'=>$_GET['bole']));
        $cont = $cno = $por = 0;
        $data = '';
        foreach($asiData as $rows){
            $cont++;
            if($rows->rmo_terminado == false){
                $cno++;
                $por = round(($cno*100)/count($asiData));
            }            
            $data[] = array('id'=>$cont,'data'=>array(
                    $cont,                                        
                    $rows->modu->mod_detalle,
                    ($rows->rmo_terminado == 0)?'../../images/icons/icon_17.png^Si':'../../images/icons/icon_24.png^No',
                    $por                                                                                                    
                ));
        }
        
        $allData = array('rows'=>$data);
        echo 'data = '.CJSON::encode($allData);
    }
}